package com.ve.file;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import com.ve.c51.R;


public class GFile {
    private boolean expand;
    private int level;
    protected Bitmap icon;
    protected int type;
    private GFile parent;
    private String name;
    private String path;

    protected boolean check;
    protected boolean directory;
    protected long size;
    protected boolean openable;
    protected boolean showPath;

    public GFile() {
        // root file
        this.level=0;
        this.name = "/";
        this.path = "/";
        this.directory = true;
        this.expand = false;
    }

    public GFile(String path) {
        this.path = path;
        if (path.endsWith("/")) {
            this.path = path.substring(0, path.length() - 1);
        }
        this.name = path.substring(path.lastIndexOf("/") + 1, path.length());

    }

    public GFile(GFile parent, String name) {
        this.parent = parent;
        this.name = name;
        this.path = (parent.isRoot() ? "" : parent.path) + "/" + name;
        this.level = parent.level + 1;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj==this){
            return true;
        }
        if (obj instanceof GFile){
            GFile file= (GFile) obj;
            return path.equals(file.path);
        }
        return false;
    }

    public void setShowPath(boolean showPath) {
        this.showPath = showPath;
    }

    public boolean isShowPath() {
        return showPath;
    }

    public boolean isOpenable() {
        return openable;
    }

    protected int initType() {
        if (directory) {
            return TYPE_DIR;
        }
        for (int i = 0; i < TYPES.length; i++) {
            for (int j = 2; j < TYPES[i].length; j++) {
                if (name.endsWith(TYPES[i][j])) {
                    return i;
                }
            }
        }
        return TYPE_UNKNOW;
    }


    public void setExpand(boolean expand) {
        this.expand = expand;
    }

    public boolean isExpand() {
        return expand;
    }

    public boolean hasParent() {
        return parent != null;
    }

    public boolean isRoot() {
        return path.endsWith("/");
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public boolean isCheck() {
        return check;
    }

    public boolean isDirectory() {
        return directory;
    }

    public int getType() {
        return type;
    }

    public int getLevel() {
        return level;
    }

    public long getSize() {
        return size;
    }

    public Bitmap getIcon() {
        return icon;
    }

    public GFile getParent() {
        return parent;
    }

    public String getName() {
        return name;
    }

    public String getPath() {
        return path;
    }

    public SpannableString getNameText() {
        if (showPath && directory) {

            return new SpannableString(path);
        }
        SpannableString ss = new SpannableString(name);
        int start = name.startsWith(".") ? 0 : name.lastIndexOf('.');
        if (start != -1 && !directory) {
            ss.setSpan(new ForegroundColorSpan(Color.parseColor("#787B83")), start, ss.length(),
                    Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    public String getSizeText() {
        double size = this.size;
        int unit = 0;
        while (size > 999) {
            size /= 999;
            unit++;
        }
        if (size < 99) {
            return String.format("%.1f", size) + SIZE_UNIT[unit];
        } else {
            return "" + (int) size + SIZE_UNIT[unit];
        }
    }

    protected static final char[] SIZE_UNIT = new char[]{'B', 'K', 'M', 'G', 'T'};
    public static final String[][] TYPES = new String[][]{
            {"文件", "unknow", "."},
            {"文件夹", "dir"},
            {"文本", "txt", ".txt", ".ini", ".conf", ".xml", ".prop", ".log"},
            {"图片", "image", ".png", ".jpg", ".jpeg", ".svg", ".bmp"},
            {"音乐", "music", ".mp3", ".wav", ".m4a", ".ogg", ".m3u"},

            {"视频", "video", ".mp4", ".mkv", ".avi", ".rmvb", ".3gp", ".mpeg"},
            {"压缩包", "zip", ".zip", ".z", ".tar", ".rar", ".gz", ".7z"},
            {"文档", "pdf", ".pdf"},
            {"网页", "html", ".html", ".htm"},
            {"安装包", "package", ".apk"},

            {"图片", "gif", ".gif"},
            {"演示", "ppt", ".ppt", ".pps"},
            {"文档", "doc", ".doc"},
            {"表格", "xls", ".xls"},
            {"C", "c", ".c", ".cpp", ".cs", ".h", ".hpp"},

            {"脚本", "script", ".sh"},
            {"代码", "code", ".java", ".js", ".json"},


    };
    public static Bitmap ICON_DIR;
    public static Bitmap ICON_FILE;
    public static Bitmap ICON_CODE;
    public static void init(Context context){
        ICON_DIR= drawableToBitmap(context.getResources().getDrawable(R.mipmap.dir));
        ICON_FILE= drawableToBitmap(context.getResources().getDrawable(R.mipmap.file));
        ICON_CODE= drawableToBitmap(context.getResources().getDrawable(R.mipmap.cfile));
    }
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int n = drawable.getIntrinsicWidth();
        int n2 = drawable.getIntrinsicHeight();
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(n, n2, config);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    public static final int TYPE_UNKNOW = 0;
    public static final int TYPE_DIR = 1;
    public static final int TYPE_TXT = 2;
    public static final int TYPE_IMAGE = 3;
    public static final int TYPE_MUSIC = 4;
    public static final int TYPE_VIDEO = 5;
    public static final int TYPE_ZIP = 6;
    public static final int TYPE_PDF = 7;
    public static final int TYPE_HTML = 8;
    public static final int TYPE_PACKAGE = 9;
    public static final int TYPE_GIF = 10;
    public static final int TYPE_PPT = 11;
    public static final int TYPE_DOC = 12;
    public static final int TYPE_XLS = 13;
    public static final int TYPE_C = 14;
    public static final int TYPE_SCRIPT = 15;
    public static final int TYPE_CODE = 16;


}
